<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>  
    
<table class="easyui-datagrid" id="viplist" title="优惠券信息" 
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'./vip/list',method:'get',pageSize:30,toolbar:'#toolbar',onClickRow: onClickRow">
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
        	<th data-options="field:'id',width:200">优惠券ID</th>
        	<th data-options="field:'uid',width:120,align:'right'">用户ID</th>
			<th data-options="field:'uname',width:200,editor:{type:'combobox',options:{
        					url:'./comm/cususer',
        					method:'get',
							valueField:'id',
							textField:'nickname',
							panelHeight:'auto',
							required:true
						}}">用户名称</th>
			<th data-options="field:'fullMoney',width:200,formatter:TAOTAO.formatPrice,editor:{type:'numberbox',options:{prompt:'输入满减条件单位为（分）',required:true}}">满减条件</th>
            <th data-options="field:'amount',width:200,formatter:TAOTAO.formatPrice,editor:{type:'numberbox',options:{prompt:'输入优惠金额单位为（分）',required:true}}">优惠券金额</th>
            <th data-options="field:'statusdate',width:130,align:'center',formatter:TAOTAO.formatVipStatus,editor:{type:'numberbox',options:{prompt:'输入期限',required:true}}">状态</th>
           	<th data-options="field:'activetimestart',width:130,align:'center',formatter:TAOTAO.formatDateTime">有效期开始时间</th>
           	<th data-options="field:'activetimeend',width:130,align:'center',formatter:TAOTAO.formatDateTime">有效期结束时间</th>
            <th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th>
        </tr>
    </thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="append()">添加</a>
 			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="accept()">保存</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="reject()">删除</a>
		</div>
		<div>
			<lable>优惠券ID:</lable>
			<input class="easyui-textbox" style="width:280px;height:25px" id="selectuser">
			<lable>用户ID:</lable>
			<input class="easyui-textbox" style="width:280px;height:25px" id="uid">
			<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="selectUserInfo()" >查询</a>
		</div>
</div>
<script>
function selectUserInfo(){
	var selectuser = $("#selectuser").val()
	var uid = $("#uid").val()
	
	$('#viplist').datagrid('load',{
		"id": selectuser,
		"uid": uid,
	});
}
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#viplist').datagrid('validateRow', editIndex)){
		$('#viplist').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}

function onClickRow(index){
	//,onClickRow: onClickRow
	if (editIndex != index){
		if (endEditing()){
			$('#viplist').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$('#viplist').datagrid('selectRow', editIndex);
		}
	}
}
function append(){
	if (endEditing()){
		$('#viplist').datagrid('appendRow',{activetimestart:new Date(),created:new Date(),updated:new Date()});
		editIndex = $('#viplist').datagrid('getRows').length-1;
		$('#viplist').datagrid('selectRow', editIndex)
				.datagrid('beginEdit', editIndex);
	}
}
function accept(){
	if (endEditing()){
		var rows = $('#viplist').datagrid('getChanges');
		var errormsg ="要保存的第【";
		for(var i =0;i<rows.length;i++){
			if(parseFloat(rows[i].amount)>parseFloat(rows[i].fullMoney)){
				errormsg +=(i+1)+"】条数据的【优惠金额("+((rows[i].amount/100).toFixed(2)+"￥")+")】不能大于【满减条件("+((rows[i].fullMoney/100).toFixed(2)+"￥")+")】！"
				alert(errormsg)
				return;
			}else if(parseFloat(rows[i].amount)===0 || parseFloat(rows[i].fullMoney)==0){
				errormsg +=(i+1)+"】条数据的【优惠金额("+((rows[i].amount/100).toFixed(2)+"￥")+")】和【满减条件("+((rows[i].fullMoney/100).toFixed(2)+"￥")+")】都可以不为0！"
				alert(errormsg)
				return;
			}
		}
		if(errormsg!=="要保存的第【"){
			return;
		}
		var rows = $('#viplist').datagrid('getChanges');

		for(var i =0;i<rows.length;i++){
			rows[i].created = new Date();
			rows[i].updated = new Date();
			$.ajax({
				 url:'./vip/andVip',
				 method:'post',
				 data:rows[i],
				 success:function(){
					$('#viplist').datagrid('acceptChanges');
					editIndex = undefined;
					$('#viplist').datagrid('reload');
				 },
				 error:function(){
						$('#viplist').datagrid('acceptChanges');
						editIndex = undefined;
					alert("更新失败") 
				 }
			 })
		}
		
	}
}
function reject(){

	var ids = getSelectionsIds()
	 $.ajax({
		 url:'./vip/delete',
		 method:'post',
		 data:{"ids":ids},
		 success:function(res){
				alert("成功") 
				$('#viplist').datagrid('reload');
		 },
		 error:function(){
			alert("重新添加，操作失败") 
		 }
	 })

}
function getSelectionsIds(){
	var itemList = $("#viplist");
	var sels = itemList.datagrid("getSelections");
	var ids = [];
	for(var i in sels){
		ids.push(sels[i].id);
	}
	ids = ids.join(",");
	return ids;
}
</script>